package org.amj.wn;


import com.google.appengine.api.users.UserServiceFactory;

public class Renderer {

	private SchemaItem model;

	public Renderer(SchemaItem model) {
		this.model = model;

	}

	public String render(WaqfeNau wn) {

		switch(model.getUiElement()) {
		case FIELD:
			
			Object value = wn.getProperty(Property.valueOf(model.getKey()));
			
			value = value == null ? "" : value;
			
			switch(model.getUiOption()) {
			case combo:
				StringBuilder sb = new StringBuilder();
				sb.append("<label><span>" + model.getCaption() + "</span> <select name='" + model.getKey() + "' class='input_select'>\r\n");

				String[] tokens = model.getValues();
				for (String token : tokens) {
					if(token.equals(value))
						sb.append("  <option value=\"" + token + "\" selected>" + token + "</option>\r\n");
					else
						sb.append("  <option value=\"" + token + "\">" + token + "</option>\r\n");
				}

				sb.append("</select></label>");

				return sb.toString();
			case label:
				//if(UserServiceFactory.getUserService().getCurrentUser() == null)
				return "<label><span>" + model.getCaption() + "</span> <input class='input_text' value='" + value + "' name='" + model.getKey() + "' type='text' disabled='true'/> </label>";
				//else
				//	return "<label><span>" + model.getCaption() + "</span> <input class='input_text' value='" + value + "' name='" + model.getKey() + "' type='text'/> <br><img src='/images/warning.png' /> Modify with caution. This field is readonly. But you can modify it because you are logged in as administrator.</label>";
			case text:
				return "<label><span>" + model.getCaption() + "</span> <input class='input_text' value='" + value + "' name='" + model.getKey() + "' type='text' /></label>";
			case textarea:
				return "<label><span>" + model.getCaption() + "</span> <textarea value='" + value + "' name='" + model.getKey() + "' class='input_text_area' cols='60' rows='10'></textarea></label>";
			case hidden:
				return "";

			}
			break;
		case PAGE:
			return "<span class='page'>" + model.getCaption() + "</span>";
		case SECTION:
			return "<span class='section'>" + model.getCaption() + "</span>";
		}
		
		
		return "org.amj.wn.Renderer.render() - Nothing to render.";
	}
}
